package springboot.HtmlModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Html {



    /**
     * @ClassName: CreateHtmlUtils
     * @Description: Java 根据模板创建 html
     * @author
     * @date 2016年4月22日 下午3:51:16
     */

        public static void main(String[] args) {
//            String filePath = "F:\\aaa.html";
            String imagePath ="http://www.baidu.com";
            String disrPath = "F:\\";
            String fileName = "aaa1";
            System.out.println(Thread.currentThread().getContextClassLoader().getResource("springboot/HtmlModel/aaa.html"));
            MakeHtml(imagePath,disrPath,fileName);
        }
        /**
         * @Title: MakeHtml
         * @Description: 创建html
//         * @param    filePath 设定模板文件
         * @param    imagePath 需要更换的url的地址
         * @param    disrPath  生成html的存放路径
         * @param    fileName  生成html名字
         * @return void    返回类型
         * @throws
         */
        public static void MakeHtml(String imagePath,String disrPath,String fileName ){
            try {
                String title = imagePath;
//                filePath = Thread.currentThread().getContextClassLoader().getResource("");
//                System.out.print(filePath);
                String templateContent = "";
                FileInputStream fileinputstream = new FileInputStream(new File(Thread.currentThread().getContextClassLoader().getResource("springboot/HtmlModel/aaa.html").getFile()));// 读取模板文件
                int lenght = fileinputstream.available();
                byte bytes[] = new byte[lenght];
                fileinputstream.read(bytes);
                fileinputstream.close();
                templateContent = new String(bytes);
                System.out.print(templateContent);
                templateContent = templateContent.replaceAll("###title###", title);
                System.out.print(templateContent);

                String fileame = fileName + ".html";
                fileame = disrPath+"/" + fileame;// 生成的html文件保存路径。
                FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立文件输出流
                System.out.print("文件输出路径:");
                System.out.print(fileame);
                byte tag_bytes[] = templateContent.getBytes();
                fileoutputstream.write(tag_bytes);
                fileoutputstream.close();
            } catch (Exception e) {
                System.out.print(e.toString());
            }
        }



}
